package cn.fl.ref.crbaseinfo.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.crbaseinfo.service.ICrBaseInfoService;
import cn.fl.ref.crbaseinfo.transform.CrAstRltvInfoVOTransform;
import cn.fl.ref.crbaseinfo.transform.CrBaseInfoVOTransform;
import cn.fl.ref.crbaseinfo.transform.CrUdtJrnlInfoVOTransform;
import cn.fl.ref.crbaseinfo.vo.CrAstRltvInfoVO;
import cn.fl.ref.crbaseinfo.vo.CrBaseInfoVO;
import cn.fl.ref.crbaseinfo.vo.CrUdtJrnlInfoVO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.ref.crbaseinfo.condition.CrBaseInfoQueryCondition;
import fl.ref.crbaseinfo.dto.CrAstRltvInfoDTO;
import fl.ref.crbaseinfo.dto.CrBaseInfoDTO;
import fl.ref.crbaseinfo.service.ICrBaseInfoApiService;
/**
 * @Description: 授信基础信息
 * @Description:业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 * 服务实现层
 */

@Service
public class CrBaseInfoServiceImpl implements ICrBaseInfoService {

	private static final Logger logger = LoggerFactory.getLogger(ICrBaseInfoService.class);
	
	@Autowired
	private ICrBaseInfoApiService  crBaseInfoApiService;

	@Override
	public CrBaseInfoVO selectById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		CrBaseInfoDTO dto = this.crBaseInfoApiService.selectById(id);
		return CrBaseInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(CrBaseInfoVO entity,List<CrBaseInfoVO> crBaseInfoHisList) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		//系统日志-有用户的情况下
		BaseLogUtils.info(logger, "add", "新增CrBaseInfo数据.", CurrentThreadContext.getCurrentUserName());
		
		//系统日志-无用户的情况下
		BaseLogUtils.info(logger, "add", "新增CrBaseInfo数据.");
		return this.crBaseInfoApiService.add(CrBaseInfoVOTransform.toDTO(entity), CrBaseInfoVOTransform.toDTOList(crBaseInfoHisList));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.crBaseInfoApiService.removeById(id);
	}

	/*@Override
	public boolean modify(CrBaseInfoVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.CrBaseInfoApiService.modify(CrBaseInfoVOTransform.toDTO(entity), null);
	}*/

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if(null == page)page = new Pager();
		return this.crBaseInfoApiService.searchListPage(page);
	}
	
	@Override
	public CrBaseInfoVO selectByIdCondition(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		CrBaseInfoDTO dto = this.crBaseInfoApiService.selectByIdCondition(id);
		return CrBaseInfoVOTransform.toVO(dto);
	}
	
	@Override
	public boolean removeCrAstRltvById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.crBaseInfoApiService.removeById(id);
	}
	
	@Override
	public boolean modify(CrBaseInfoVO crBaseInfoEntity, List<CrBaseInfoVO> crBaseInfoHisList,CrUdtJrnlInfoVO crUdtJrnlInfoEntity) throws BaseException {
		if(null == crBaseInfoEntity||null == crUdtJrnlInfoEntity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.crBaseInfoApiService.modify(CrBaseInfoVOTransform.toDTO(crBaseInfoEntity), CrBaseInfoVOTransform.toDTOList(crBaseInfoHisList), CrUdtJrnlInfoVOTransform.toDTO(crUdtJrnlInfoEntity));
	}

	@Override
	public boolean removeByCrId(String crId) throws BaseException {
		if(StringUtils.isEmpty(crId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.crBaseInfoApiService.removeByCrId(crId);
	}

	@Override
	public List<CrBaseInfoVO> selectByCondition(CrBaseInfoQueryCondition qc) throws BaseException {
		if(null == qc){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		List<CrBaseInfoDTO> CrBaseInfoDTOEntity=this.crBaseInfoApiService.selectByCondition(qc);
		return CrBaseInfoVOTransform.toVOList(CrBaseInfoDTOEntity);
	}
	
	@Override
	public List<CrBaseInfoVO> selectHisCrList(CrBaseInfoQueryCondition qc) throws BaseException {
		if(null == qc){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		List<CrBaseInfoDTO> CrBaseInfoDTOEntity=this.crBaseInfoApiService.selectHisCrList(qc);
		return CrBaseInfoVOTransform.toVOList(CrBaseInfoDTOEntity);
	}
	
	@Override
	public List<CrBaseInfoVO> selectHisCrListValid(CrBaseInfoQueryCondition qc) throws BaseException {
		if(null == qc){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		List<CrBaseInfoDTO> CrBaseInfoDTOEntity=this.crBaseInfoApiService.selectHisCrListValid(qc);
		return CrBaseInfoVOTransform.toVOList(CrBaseInfoDTOEntity);
	}

	@Override
	public boolean modify(CrBaseInfoVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.crBaseInfoApiService.modify(CrBaseInfoVOTransform.toDTO(entity));
	}

	@Override
	public CrBaseInfoVO selectByCrId(String crId) throws BaseException {
		CrBaseInfoDTO dto = crBaseInfoApiService.selectByCrId(crId);
		CrBaseInfoVO vo = CrBaseInfoVOTransform.toVO(dto);
		return vo;
	}
	
	@Override
	public DataResultDTO addFlowStart(FlowStartParamsDTO flowStartParamsDTO) throws BaseException{
		if(StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())){
			BaseLogUtils.info(logger, "addFlowStart", "用户ID和业务数据主键必须要存在!",CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return this.crBaseInfoApiService.addFlowStart(flowStartParamsDTO);
	}
	
	@Override
	public List<CrAstRltvInfoVO>  selectAstInfoByCrId(int rvlCr, String crId) throws BaseException{
		if(StringUtils.isEmpty(rvlCr)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "授信循环类型不能为空");
		}
		if(StringUtils.isEmpty(crId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "授信编号不能为空");
		}
		List<CrAstRltvInfoDTO> entityList=this.crBaseInfoApiService.selectAstInfoByCrId(rvlCr, crId);
		return CrAstRltvInfoVOTransform.toVOList(entityList);
	}
	
	@Override
	public boolean selectCnkiCrCtrNo(String crCtrNo) throws BaseException{
		if(StringUtils.isEmpty(crCtrNo)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "授信合同号不能为空");
		}
		return this.crBaseInfoApiService.selectCnkiCrCtrNo(crCtrNo);
	}
	
	@Override
	public boolean selectCnkiCrNm(String crNm) throws BaseException{
		if(StringUtils.isEmpty(crNm)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "授信名称不能为空");
		}
		return this.crBaseInfoApiService.selectCnkiCrNm(crNm);
	}
	
	@Override
	public boolean batchUpdateCrStSTOP(int crSt) throws BaseException{
		if(StringUtils.isEmpty(crSt)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "授信名称不能为空");
		}
		return this.crBaseInfoApiService.batchUpdateCrStSTOP(crSt);
	}
	
	/**
	    * 获取机构限额信息
	 * @return
	 */
	public List<Map<String, Object>> searchFundRasInsLimList(Long insId) throws BaseException {
		return this.crBaseInfoApiService.searchFundRasInsLimList(insId);
	}
}
